define([
		"jquery"

	],
	function($) {
		$.sys = {
			/**
			 * 消息框队列
			 */
			messageQueue: [],
			messageTemplate: $('<div class="alert alert-dismissible"><button type="button" class="close">&times;</button></div>'),
			content : "#content",//主工作区域
			/**
			 *判断错误页面的选择器
			 */
			errorPage: "._errorPage",
			errorHtml:'<div style="margin-top:10px;font-size: 3em;" class="text-center text-danger"><i class="fa fa-2x fa-warning"></i> 页面加载错误<br/><button class="btn btn-danger"><i class="fa fa-refresh"></i> 重新加载</button></div>',
			loading:'<div class="loading"><i class="fa fa-spinner fa-pulse fa-fw"></i></div>',
			/**
			 * 显示消息
			 * @param {String} msg 消息内容
			 * @param {Number} timeout 自动消失毫秒 默认 3000
			 * @param {String} level 消息等级 success info(默认) warning danger
			 */
			m: function(msg, timeout, level) {
				var current = this;
				timeout = timeout || 5000;
				level = level || "info";
				var div = this.messageTemplate.clone();
				div.addClass("alert-" + level)
					.append(msg)
					.appendTo($("body"));
				var height = div.outerHeight();
				div.addClass("animated bounceInUp");
				$.each(current.messageQueue, function(i, v) {
					v.data("b", v.data("b") + height + 5);
					v.stop();
					v.animate({
						bottom: v.data("b")
					}, 1000);
				});
				window.setTimeout(function() {
					div.removeClass("animated bounceInUp");
				}, 1000);
				div.css({
					position: "fixed",
					"z-index": 99999,
					right: 0,
					bottom: 0,
					margin: "5px"
				}).data("b", 0);
				this.messageQueue.push(div);

				var timer = null;
				/**
				 * 移除消息
				 * @param {Object} div
				 */
				var removeMessage = function(div) {
					if(timer != null) {
						window.clearTimeout(timer);
					}
					var index = current.messageQueue.indexOf(div);
					if(index > -1) {
						var height = div.outerHeight();
						for(var i = 0; i < index; i++) {
							var v = current.messageQueue[i];
							v.data("b", v.data("b") - height - 5);
							v.stop();
							v.animate({
								bottom: v.data("b")
							}, 1000);
						}
						current.messageQueue.splice(index, 1);
						div.addClass("animated bounceOutDown");
						window.setTimeout(function() {
							div.remove();
						}, 1000);
					}
				}

				div.children(".close").click(function() {
					removeMessage(div);
				});

				if(timeout > 0) {
					timer = window.setTimeout(function() {
						removeMessage(div);
					}, timeout + 1000);
				}
				return div;
			},
			/**
			 * 成功消息
			 * @param {String} msg
			 * @param {Number} timeout
			 */
			s: function(msg, timeout) {
				return this.m(msg, timeout, "success");
			},
			/**
			 * 一般消息
			 * @param {String} msg
			 * @param {Number} timeout
			 */
			i: function(msg, timeout) {
				return this.m(msg, timeout, "info");
			},

			/**
			 * 警告消息
			 * @param {String} msg
			 * @param {Number} timeout
			 */
			w: function(msg, timeout) {
				return this.m(msg, timeout, "warning");
			},

			/**
			 * 错误消息
			 * @param {String} msg
			 * @param {Number} timeout
			 */
			e: function(msg, timeout) {
				return this.m(msg, timeout, "danger");
			},
			
			/**
			 * 加载HTML
			 * @param {String} url
			 * @param {Array} postData
			 * @param {String} context
			 * @param {Function} fn 如果fn不传递，则加载对应的js并执行init(content),如果find(errorPage)，则不执行
			 */
			html: function(url, postData, context, fn) {
				if(postData == null) postData = [];
				context = $(context);
				context.html($.sys.loading);
				$.ajax({
					url: url,
					dataType: "html",
					type: "post",
					data: postData,
					success: function(data) {
						context.html(data);
						$.sys.parse(context);
						
						if($.isFunction(fn)) {
							fn(context);
						} else if(context.find($.sys.errorPage).length == 0) {
							var js = url.substring(0, url.lastIndexOf("."));
							require([js], function(_) {
								_.init(context);
							});
						}

					},
					error: function(data) {
						context.html($.sys.errorHtml);
						context.find("button").click(function() {
							$.sys.html(url, postData, context, fn);
						});
					}
				});
			},

			/**
			 * 自动进度条
			 */
			progress:{
				progressObj:null,
				create:function(){
					if(this.progressObj==null){
						var messageDiv = $.sys.w('<span></span><br /><div class="progress"><div class="progress-bar progress-bar-danger progress-bar-striped active" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">0%</div></div>',-1);
						messageDiv.css("min-width","30em");
						messageDiv.children(".close").css("position","fixed;").css("top","-1000em");
						messageDiv.css("padding-right","15px");
						messageDiv.find(".progress").css("margin","5px 0");
						this.progressObj = messageDiv;
					}
					return this.progressObj;
					
				},setTitle:function(title){
					this.create().find("span").text(title);
				},
				set:function(p){
					p=p*100;
					var messageDiv = this.create();
					var ps = ["progress-bar-danger","progress-bar-warning","progress-bar-info","progress-bar-success"];
					var pb = messageDiv.find(".progress-bar");
					var index=Math.floor(p/(100/ps.length));
					if(index>=ps.length){
						index = ps.length-1
					}
					pb.addClass(ps[index]);
					if(index>0){
						pb.removeClass(ps[index-1]);
					}
					pb.css("width",p+"%").text(p.toFixed(1)+"%");
				},
				done:function(){
					if(this.progressObj!=null){
						this.progressObj.children(".close").trigger("click");
						this.progressObj=null
					}
				}
				
			}
			//自动进度条结束
		}
		return $.sys;
});